<?php
/**
 *
 * UCModMedal class file
 *
 * @category   UC
 * @author     刘海涛 <liuhaitao01@snda.com>
 * @copyright  1997-2010 SDO Group
 * @version    Release: 1.0.0
 * @link       http://www.sdo.com
 * @license    http://www.sdo.com
 */
require_once(dirname(__FILE__).'/../../libraries/UCLibPDOFactory.php');
require_once(dirname(__FILE__).'/../../config/UCConfMedal.php');

class UCModMedal
{
	public $error_no = '';
	private $_user_id = 0;
	
	/**
	 * 构造函数
	 *
	 */
	function __construct($user_id)
	{
		if(empty($user_id))
		{
			$this->error_no = UCConfMedal::MEDAL_ERROR_PARAM;
			return false;
		}
		$this->_user_id = $user_id;
	}
	
	/**
	 * 析构函数
	 *
	 */
	function __destruct()
	{
	}
	
	/**
	 * 添加用户勋章流水
	 * @param	string	$ar_param	array(	'medal_id'		=> '勋章ID',
											'medal_status'	=> '勋章状态',
											'medal_limit'	=> '勋章获取限制',
										 )
	 * @return	int					正常返回流水ID 错误返回错误ID
	 *
	 */
	function addMedal($ar_param)
	{
		if(!is_array($ar_param) || count($ar_param)<1)
		{
			$this->error_no = UCConfMedal::MEDAL_ERROR_PARAM;
			return false;
		}
		$ar_param['user_id'] = $this->_user_id;

		$ar_param['create_time'] = date('Y-m-d H:i:s');
		$ar_param['modify_time'] = date('Y-m-d H:i:s');
		$ar_param['ip'] = ip2long(get_client_ip());
		$s_key = 'medal_type';
		$s_value = '?';
		$ar_tmp[] = UCConfMedal::$ar_medal_info[$ar_param['medal_id']]['type'];
		$o_pdo	= UCLibPDOFactory::getPDO(UCConfMedal::MEDAL_DB_FLAG,$ar_param['user_id'],'medal2dbconfig');
		$ar_param['um_id'] = $o_pdo->gen_uuid();
		foreach($ar_param as $key=>$value)
		{
			$s_key		.= ',' . $key;
			$s_value	.= ',?';
			$ar_tmp[]	=  $value;
		}
		$s_sql	= 'INSERT INTO ' . $o_pdo->database.'.' . $o_pdo->table. ' (' . trim($s_key,',') . ') VALUES (' . trim($s_value,',') . ')';
		$ret = $o_pdo->exec_with_prepare($s_sql,$ar_tmp);
		if(!$ret)
		{
			$this->error_no = UCConfMedal::MEDAL_ERROR_DB;
			return false;
		}
		return $ar_param['um_id'];
	}

	/**
	 * 获取用户单个勋章信息通过用户ID和勋章ID
	 * @param	int		$medal_id	勋章ID
	 * @return	array				勋章详细信息
	 *
	 */
	function getOneMedalByMedalid($medal_id)
	{
		if(empty($medal_id))
		{
			$this->error_no = UCConfMedal::MEDAL_ERROR_PARAM;
			return false;
		}
		$o_pdo	= UCLibPDOFactory::getPDO(UCConfMedal::MEDAL_DB_FLAG,$this->_user_id,'medal2dbconfig');
		$s_sql	= 'SELECT * FROM ' . $o_pdo->database.'.' . $o_pdo->table. ' WHERE user_id=? AND medal_id=?';
		return $o_pdo->query_one_row_with_prepare($s_sql,array($this->_user_id,$medal_id));
	}

	/**
	 * 获取用户单个勋章信息通过流水ID
	 * @param	string		$id		勋章流水号
	 * @return	array				勋章详细信息
	 *
	 */
	function getOneMedalById($id)
	{
		if(empty($id))
		{
			$this->error_no = UCConfMedal::MEDAL_ERROR_PARAM;
			return false;
		}
		$o_pdo	= UCLibPDOFactory::getPDO(UCConfMedal::MEDAL_DB_FLAG,$this->_user_id,'medal2dbconfig');
		$s_sql	= 'SELECT * FROM ' . $o_pdo->database.'.' . $o_pdo->table. ' WHERE um_id=?';
		return $o_pdo->query_one_row_with_prepare($s_sql,array($id));
	}

	/**
	 * 获取用户勋章列表信息
	 * @param	int		$status	勋章状态
	 * @return	array			勋章列表信息
	 *
	 */
	function getAllMedal($status)
	{
		$o_pdo	= UCLibPDOFactory::getPDO(UCConfMedal::MEDAL_DB_FLAG,$this->_user_id,'medal2dbconfig');
		$s_sql	= 'SELECT * FROM ' . $o_pdo->database.'.' . $o_pdo->table. ' WHERE user_id=? AND medal_status>? ORDER BY modify_time DESC';
		return $o_pdo->query_with_prepare($s_sql,array($this->_user_id,intval($status)));
	}

	/**
	 * 修改用户勋章信息
	 * @param	string	$id			勋章流水ID
	 * @param	array	$ar_param	array(	'medal_id'		=> '勋章ID',
											'medal_status'	=> '勋章状态',
											'medal_limit'	=> '勋章获取限制',
										 )
	 * @return	
	 *
	 */
	function updateMedalInfo($id,$ar_param)
	{
		if(!is_array($ar_param) || count($ar_param)<1 || empty($id))
		{
			$this->error_no = UCConfMedal::MEDAL_ERROR_PARAM;
			return false;
		}

		$ar_param['modify_time'] = date('Y-m-d H:i:s');
		foreach($ar_param as $key=>$value)
		{
			$s_update	.= ',' . $key . '=?';
			$ar_tmp[]	= $value;
		}
		$o_pdo	= UCLibPDOFactory::getPDO(UCConfMedal::MEDAL_DB_FLAG,$this->_user_id,'medal2dbconfig');
		$s_sql	= 'UPDATE ' . $o_pdo->database.'.' . $o_pdo->table. ' SET '. trim($s_update,',') .' WHERE um_id=?';
		$ar_tmp[] = $id;
		return $o_pdo->exec_with_prepare($s_sql,$ar_tmp);
	}
}
?>